package com.cs.dao;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.cs.entity.Admin;
import com.cs.entity.Page;
import com.cs.entity.Student;
import com.cs.entity.Teacher;

@Repository
@Transactional
public class AdminDaoImpl {
	@Resource
	private SessionFactory sessionFactory;
	
	//管理员登录
	public Admin admLogin(String aname,String apassword){
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Admin where aname=? and apassword=?");
		query.setParameter(0, aname);
		query.setParameter(1, apassword);
		Admin admin = (Admin) query.uniqueResult();
		return admin;
	}
	//学生管理
	//查询全部学生并分页显示
	public Page<Student> findAllStudent(int pageNum,int pageSize){
		Query query1 = this.sessionFactory.getCurrentSession().createQuery("from Student");
		Query query2 = this.sessionFactory.getCurrentSession().createQuery("select count(id) from Student");
		int count = ((Number)query2.uniqueResult()).intValue();
		query1.setFirstResult((pageNum-1)*pageSize);
		query1.setMaxResults(pageSize);
		List<Student> list = query1.list();
		Page<Student> sPage=new Page<>();
		sPage.setList(list);
		sPage.setPageSize(pageSize);
		sPage.setPageNum(pageNum);
		sPage.setTotalCount(count);
		return sPage;
	}
	//根据学号查询学生
	public Student findStudentBySno(String sno) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Student where sno=?");
		query.setParameter(0, sno);
		Student student = (Student) query.uniqueResult();
		return student;
	}
	//添加学生
	public int addStudent(Student student){
		int save = (int) this.sessionFactory.getCurrentSession().save(student);
		return save;
	}
	//删除学生（根据id）
	public boolean delStudent(Integer id) {
		Student student = this.sessionFactory.getCurrentSession().load(Student.class, id);
		this.sessionFactory.getCurrentSession().delete(student);
		return true;
	}
	//查询全部教师并分页
	public Page<Teacher> findAllTeacher(int pageNum,int pageSize){
		Query query1 = this.sessionFactory.getCurrentSession().createQuery("from Teacher");
		Query query2 = this.sessionFactory.getCurrentSession().createQuery("select count(id) from Teacher");
		int count = ((Number)query2.uniqueResult()).intValue();
		query1.setFirstResult((pageNum-1)*pageSize);
		query1.setMaxResults(pageSize);
		List<Teacher> list = query1.list();
		Page<Teacher> sPage=new Page<>();
		sPage.setList(list);
		sPage.setPageSize(pageSize);
		sPage.setPageNum(pageNum);
		sPage.setTotalCount(count);
		return sPage;
	}
	//根据教工号查询教师
	public Teacher findTeacherByTno(String tno) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Teacher where tno=?");
		query.setParameter(0, tno);
		Teacher teacher = (Teacher) query.uniqueResult();
		return teacher;
	}
	//添加教师
	public int addTeacher(Teacher teacher){
		int save = (int) this.sessionFactory.getCurrentSession().save(teacher);
		return save;
	}
	//删除教师
	public boolean delTeacher(Integer id) {
		Teacher teacher = this.sessionFactory.getCurrentSession().load(Teacher.class, id);
		this.sessionFactory.getCurrentSession().delete(teacher);
		return true;
	}

}
